package cn.innosmart.aq.customize.shareimage;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.innosmart.aq.R;
import cn.innosmart.aq.util.ImageComTools;

/**
 * Created by David on 2015/12/7.
 */
public class SingleImageSelectView extends RelativeLayout {
    private Drawable originImage;
    private Drawable cancelImage;
    private ImageSelect imageSelect;
    private ImageView originImageView;
    private ImageView cancelImageView;
    private Context context;
    private OnClickListener onOriginImageOnClickListener;


    public SingleImageSelectView(Context context) {
        this(context, null);
    }


    public SingleImageSelectView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public SingleImageSelectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SingleImageSelectView, defStyleAttr, 0);
        int index = a.getIndexCount();
        for (int i = 0; i < index; i++) {
            int atrr = a.getIndex(i);
            switch (atrr) {
                case R.styleable.SingleImageSelectView_shareImageOrigin:
                    originImage = a.getDrawable(atrr);
                    break;
                case R.styleable.SingleImageSelectView_orignImagePadding:
                    break;
                case R.styleable.SingleImageSelectView_shareImageCancel:
                    cancelImage = a.getDrawable(atrr);
                    break;
                case R.styleable.SingleImageSelectView_cancelImagePadding:
                    break;
            }
        }
        a.recycle();
        this.context = context;
        imageSelect = new ImageSelect();
        LayoutInflater.from(context).inflate(R.layout.customsize_single_image_select, this, true);
        originImageView = (ImageView) findViewById(R.id.iv_origin);
        cancelImageView = (ImageView) findViewById(R.id.iv_cancel);
        if (originImage != null) {
            originImageView.setImageDrawable(originImage);
        } else {
            originImageView.setBackgroundResource(android.R.drawable.ic_menu_camera);
        }

    }

    public void setOnImageClickListener(OnClickListener onClickListener) {
        this.onOriginImageOnClickListener = onClickListener;
        originImageView.setOnClickListener(onOriginImageOnClickListener);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        WindowManager a = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point size = new Point();
        a.getDefaultDisplay().getSize(size);
        int parentX = size.x;
        int parentY = size.y;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = parentX / 3;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = width;
        }
        setMeasuredDimension(width, height);
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public boolean getStatus() {
        return imageSelect.isSelect();
    }


    public void setImage(String url) {
        imageSelect.setSelect(true);
        imageSelect.setUrl(url);
        Bitmap rawBitmap1 = ImageComTools.scaleBitmap(url, 640);
        originImageView.setImageBitmap(rawBitmap1);
        cancelImageView.setImageDrawable(cancelImage);
    }


    private class ImageSelect {
        private boolean isSelect;
        private String url;
        public ImageSelect() {
        }

        public ImageSelect(boolean isSelect, String url) {
            this.isSelect = isSelect;
            this.url = url;
        }

        public boolean isSelect() {
            return isSelect;
        }

        public void setSelect(boolean select) {
            isSelect = select;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
